# -*- coding: utf-8 -*-
# @Date   : 2021/03/31
# @File   : CF01_ReceiptYears.py
# @Author : zhanghengfeng

'''eof
name:收汇率年份数
code:CF01_ReceiptYears
tableName:
columnName:
groups:出口信息模块
dependencies:CF_CQ_DSJ
type:常用指标
dataSourceType:
eof'''

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']


def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

def CF01_ReceiptYears(): 
    data = CF_CQ_DSJ.get('body')
    try:
        #基本信息
        baseInfo = data.get('baseInfo')
        #年度收汇列表
        exchangeRate = baseInfo[0].get('exchangeRate')
        if exchangeRate in null_type_list:
            return u'缺失值'
        elif exchangeRate == []:
            return 0
        elif isinstance(exchangeRate,list) and len(exchangeRate)>0:
            errorConts = 0
            conts = 0
            for i in exchangeRate:
                try:
                    #收汇率
                    collectRate = i.get('collectRate')
                    #收汇年份
                    year = i.get('year')
                    if isfloat(collectRate) and isfloat(year) and isinstance(int(year),int) and len(year) == 4:
                        if float(collectRate) > 0:
                            conts += 1
                        else:
                            pass
                    else:
                        errorConts += 1
                except:
                    errorConts += 1
            if errorConts == len(exchangeRate):
                return u'缺失值'
            else:
                return conts
        else:
            return u'缺失值'
    except:
        return u'缺失值'

result = CF01_ReceiptYears()